package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.BdmGood;
import com.ruoyi.system.domain.BdmShareOutBonusLevel;
import com.ruoyi.system.domain.dto.BdmGoodDto;
import com.ruoyi.system.domain.dto.BdmShareOutBonusLevelDto;
import com.ruoyi.system.domain.vo.BdmShareOutBonusLevelVo;
import com.ruoyi.system.enums.BdmGoodEnum;
import com.ruoyi.system.mapper.BdmShareOutBonusLevelMapper;
import com.ruoyi.system.service.IBdmGoodService;
import com.ruoyi.system.service.IBdmShareOutBonusLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 分红池等级 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-17
 */
@Service
public class BdmShareOutBonusLevelServiceImpl extends ServiceImpl<BdmShareOutBonusLevelMapper, BdmShareOutBonusLevel> implements IBdmShareOutBonusLevelService {


    @Autowired
    private IBdmGoodService iBdmGoodService;

    /**
     * 新增分红池
     *
     * @param dto
     * @return
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public boolean saveShareOutBonusLevel(BdmShareOutBonusLevelDto dto) {
        List<BdmShareOutBonusLevel> list = this.lambdaQuery().list();
        if (!CollectionUtils.isEmpty(list)) {
            throw new RuntimeException("目前奖金池只能有一个,如有变动请修改或删除已存在的数据");
        }
        if (CollectionUtils.isEmpty(dto.getProductList())) {
            throw new RuntimeException("请关联商品");
        }
        BdmShareOutBonusLevel bonusLevel = new BdmShareOutBonusLevel();
        BeanUtils.copyProperties(dto, bonusLevel, "id");
        bonusLevel.setCreateTime(LocalDateTime.now());
        List<BdmGoodDto> productList = dto.getProductList();

        iBdmGoodService.updateBatchById(productList.stream().map(item -> {
            BdmGood bdmGood = new BdmGood();
            bdmGood.setId(item.getId());
            bdmGood.setPrizePool(item.getPrizePool());
            bdmGood.setUpdateTime(new Date());
            return bdmGood;
        }).collect(Collectors.toList()));
        bonusLevel.setProductIds(productList.stream().map(BdmGoodDto::getId).map(Object::toString).collect(Collectors.joining(",")));
        this.save(bonusLevel);
        return true;
    }

    /**
     * 修改分红池
     *
     * @param dto
     * @return
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public boolean updateShareOutBonusLevel(BdmShareOutBonusLevelDto dto) {
        Long id = dto.getId();
        this.removeById(id);
        iBdmGoodService.lambdaUpdate().set(BdmGood::getPrizePool, BigDecimal.ZERO).update();
        this.saveShareOutBonusLevel(dto);
        return true;
    }

    /**
     * 获取分红池详情q
     * @param id
     * @return
     */
    @Override
    public BdmShareOutBonusLevelVo getShareOutBonusLevelById(Long id) {
        BdmShareOutBonusLevelVo vo = new BdmShareOutBonusLevelVo();
        BdmShareOutBonusLevel share = this.getById(id);
        BeanUtils.copyProperties(share,vo);
        String productIds = share.getProductIds();
        if (StringUtils.isNotBlank(productIds)){
            List<String> ids = Arrays.stream(productIds.split(",")).collect(Collectors.toList());
            List<BdmGood> goodList = iBdmGoodService.lambdaQuery().in(BdmGood::getId, ids).list();
            if (!CollectionUtils.isEmpty(goodList)){
                vo.setProductList(goodList.stream().map(i->{
                    BdmGoodDto bdmGoodDto = new BdmGoodDto();
                    BeanUtils.copyProperties(i,bdmGoodDto);
                    return bdmGoodDto;
                }).collect(Collectors.toList()));
            }
        }
        return vo;
    }

    @Override
    public void addShareOutBonusLevelMoney(String orderId) {
        baseMapper.addShareOutBonusLevelMoney(orderId);
    }
}
